<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\CategoryModel;
use app\model\StaffRoleModel;
use app\model\UserModel;
use think\facade\Request;
use think\facade\Validate;

class StaffRole
{
    //角色列表获取
    public function list()
    {
        $staffRoleList = StaffRoleModel::select();
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'staffRoleList' => $staffRoleList,
            ],
        ];
        return json($data);
    }
    public function add()
    {

        //order增加角色
        $params = Request::param();
        $validate = Validate::rule([
            'role_name|角色名称' => 'require',
//            'password|密码' => 'require',
//            'role_id|角色id' => 'require',
//            'staff_id|员工id' => 'require'
        ]);
        if (!$validate->check(['role_name'=>$params['role_name']])) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
        $params['add_time']=$params['update_time']=time();
        $result=StaffRoleModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    // 角色删除
    public function delete()
    {
        $roleId = Request::param("role_id");
        $validate = Validate::rule([
            'role_id|角色id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['role_id' => $roleId])) {
            echo $validate->getError();
            exit();
        }
//        $role=StaffRoleModel::find($roleId);
        $result = StaffRoleModel::destroy($roleId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //角色修改
    public function update()
    {
        $params = Request::param();
        $validate = Validate::rule([
            'role_id|角色id'=>'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['role_id'=>$params['role_id']])) {
            echo $validate->getError();
            exit();
        }

        $staffrole = StaffRoleModel::find($params['role_id']);
//
        if ($params['role_name']){
            $staffrole['role_name']=$params['role_name'];
        }
//        if ($params['password']){
//            $staff['password']=$params['password'];
//        }
//        if ($params['role_id']){
//            $staff['role_id']=$params['role_id'];
//        }
        $staffrole['update_time'] = time();
        $result = $staffrole->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
